package com.cxx.home.service.impl;

import com.cxx.framework.web.CustomException;
import com.cxx.home.dao.LedgerDao;
import com.cxx.home.dto.ledger.LedgerDto;
import com.cxx.home.entity.Ledger;
import com.cxx.home.mapper.LedgerMapper;
import com.cxx.home.service.LedgerService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LedgerServiceImpl implements LedgerService {
    @Resource
    private LedgerDao ledgerDao;

    @Resource
    private LedgerMapper ledgerMapper;

    @Override
    public Boolean addLedger(LedgerDto ledgerDto) {
        Ledger ledger = new Ledger();
        BeanUtils.copyProperties(ledgerDto, ledger);

        return ledgerMapper.insert(ledger) == 1;
    }

    @Override
    public Boolean updateLedger(Long id, LedgerDto ledgerDto) {
        if (ledgerMapper.selectById(id) == null) {
            throw new CustomException("该账本内容不存在");
        }

        Ledger ledger = new Ledger();
        BeanUtils.copyProperties(ledgerDto, ledger);
        ledger.setId(id);
        return ledgerMapper.updateById(ledger) == 1;
    }

    @Override
    public Boolean deleteLedger(Long id) {
        if (ledgerMapper.selectById(id) == null) {
            throw new CustomException("该绩效不存在");
        }

        return ledgerMapper.deleteById(id) == 1;
    }

    @Override
    public List<LedgerDto> queryLedger(String book) {
        return ledgerDao.queryLedger(book);
    }

    @Override
    public List<String> queryLedgerBook() {
        return ledgerDao.queryLedgerBook();
    }
}
